package org.jsola.hr.provider.impl;

import org.apache.commons.lang3.StringUtils;
import org.jsola.core.Page;
import org.jsola.hr.dto.TypeAndId;
import org.jsola.hr.entity.EmpInfoDO;
import org.jsola.hr.provider.IInsuranceProviderService;
import org.jsola.hr.service.IChildCompanyService;
import org.jsola.hr.service.IEmpInfoService;
import org.jsola.hr.vo.UserCompanyVO;
import org.jsola.insurance.query.EmpInsuranceRecordQuery;
import org.jsola.insurance.query.QyInsuranceRecordQuery;
import org.jsola.insurance.service.IQyInsuranceRecordService;
import org.jsola.insurance.vo.JfStatisticsAnalyseVO;
import org.jsola.insurance.vo.QyInsuranceRecordVO;
import org.jsola.insurance.vo.RecordListStatisticsVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author lyh
 */
@Service("IHrInsuranceProviderServiceImpl")
public class InsuranceProviderServiceImpl implements IInsuranceProviderService {

    @Autowired
    private IQyInsuranceRecordService insuranceRecordService;

    @Autowired
    private IChildCompanyService childCompanyService;

    @Autowired
    private IEmpInfoService empInfoService;

    @Override
    public Page<QyInsuranceRecordVO> selectPage(QyInsuranceRecordQuery query, TokenUser tokenUser) {
        query.setCompanyIdList(this.getCompanyIdList(query, tokenUser));
        return insuranceRecordService.selectPage(query, tokenUser);
    }

    @Override
    public String exportRecordList(QyInsuranceRecordQuery query, TokenUser tokenUser) {
        query.setCompanyIdList(this.getCompanyIdList(query, tokenUser));
        return insuranceRecordService.exportRecordList(query, tokenUser);
    }

    @Override
    public RecordListStatisticsVO recordListStatistics(QyInsuranceRecordQuery query, TokenUser tokenUser) {
        query.setCompanyIdList(this.getCompanyIdList(query, tokenUser));
        return insuranceRecordService.recordListStatistics(query, tokenUser);
    }

    @Override
    public List<QyInsuranceRecordVO> selectEmpInsuranceList(String empId, TokenUser tokenUser) {
        EmpInfoDO empInfo = empInfoService.selectById(empId);
        List<QyInsuranceRecordVO> selectResult;
        if (!StringUtils.isEmpty(empInfo.getIdCard())) {
            selectResult = insuranceRecordService.selectByIdCard(empInfo.getIdCard(), tokenUser.getSiteId());
        } else {
            EmpInsuranceRecordQuery query = new EmpInsuranceRecordQuery();
            query.setEmpIdList(Collections.singletonList(empInfo.getId()));
            selectResult = insuranceRecordService
                    .selectEmpInsurance(query, tokenUser.getSiteId());
        }
        // 需要优先显示在保人员 并且按截止时间倒序排序
        List<QyInsuranceRecordVO> insIng = selectResult.stream()
                .filter(record -> record.getBeginTime().compareTo(new Date()) <= 0 &&
                        record.getEndTime().compareTo(new Date()) >= 0)
                .sorted(Comparator.comparing(QyInsuranceRecordVO::getEndTime).reversed())
                .collect(Collectors.toList());
        List<QyInsuranceRecordVO> insOther = selectResult.stream()
                .filter(record -> !(record.getBeginTime().compareTo(new Date()) <= 0 &&
                        record.getEndTime().compareTo(new Date()) >= 0))
                .sorted(Comparator.comparing(QyInsuranceRecordVO::getEndTime).reversed())
                .collect(Collectors.toList());
        insIng.addAll(insOther);
        return insIng;
    }

    private List<String> getCompanyIdList(QyInsuranceRecordQuery query, TokenUser tokenUser) {
        TypeAndId typeAndId = new TypeAndId();
        typeAndId.setType(query.getType().getType());
        typeAndId.setId(query.getType().getId());
        UserCompanyVO userCompany = childCompanyService.selectByUserId(tokenUser.getUserId(), tokenUser.getSiteId());
        Map<String, List<String>> stringListMap = childCompanyService
                .filterCustomerDetailCompanyIds(typeAndId, userCompany, tokenUser.getSiteId());
        return stringListMap.get("permissionGroupAndCompanyIds");
    }

    @Override
    public JfStatisticsAnalyseVO jfStatisticsAnalyseInsurance(QyInsuranceRecordQuery query, TokenUser tokenUser) {
        return insuranceRecordService.jfStatisticsAnalyseInsurance(query, tokenUser);
    }
}
